#!/bin/ksh

fortran_compile=$FC
if [[ $FC = '' ]]
then echo "set an environment variable FC to the fortran_compile_command like f90"
     echo "or do all compilation first and comment the compilation lines"
     exit
fi

label='GHCN.CL.PA' ; rad=1200

i="to_next_step/Ts.${label}"

if [[ ! -s $i.1 ]] ; then echo "input files ${i}.1-6 missing"; exit; fi

##  Input files:
n=1
while [[ $n -le 6 ]]
do
   ln ${i}.$n  fort.3$n
   (( n=$n + 1 ))
done

${fortran_compile} to.SBBXgrid.f -o to.exe
to.exe 1880 $rad > to.SBBXgrid.1880.$label.$rad.log

##   Output files:

echo "The following files were created:"
echo "SBBX1880.Ts.${label}.$rad    BX.Ts.${label}.$rad  "
mv fort.10 SBBX1880.Ts.${label}.$rad
mv fort.11   BX.Ts.${label}.$rad
mv fort.77   statn.use.Ts.${label}.$rad

./zonav $label

./trimSBBX SBBX1880.Ts.${label}.$rad

# Clean-up
rm fort.3[0-6] ; rm  to.exe
mv *exe *log *use* work_files/.
a=$( ls SBBX*.trim ) ; mv $a to_next_step/${a%.trim}
mv BX* SBBX* work_files/.
